package com.pudu.mydemo.entity;

/**
 * Created by lgq on 2016/8/25.
 */

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.pudu.pdrobot.App;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 开启一个循环队列来接收日志内容，并且写入文件
 */
public class LogFileThread extends Thread{

    private static String MYLOGFILEName = "Log.txt";// 本类输出的日志文件名称

    public static Handler mHandler;
    public static Handler getmHandler(){
        return mHandler;
    }
    @Override
    public void run() {
        Looper.prepare();
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                Bundle b = msg.getData();
                /**
                 * MYLOG_PATH_SDCARD_DIR = "/sdcard/" ; file路径
                 * fileDateName = "2016-08-15 + tag" ;  获得当天的日期
                 * MYLOGFILEName = "Log.txt" ;   固定的命名
                 */
                String fileDateName = b.getString("tagString");
                String fileInfo = b.getString("explainString");
                File file = new File(App.getInstance().getLogPath(), fileDateName + "-" +  MYLOGFILEName);
                try {
                    if(file.exists() == false)
                    {
                        file.createNewFile();
                    }

                    FileWriter filerWriter = new FileWriter(file, true);//后面这个参数代表是不是要接上文件中原来的数据，不进行覆盖,追加在内容后面
                    BufferedWriter bufWriter = new BufferedWriter(filerWriter);
                    bufWriter.write(fileInfo);
                    bufWriter.newLine();
                    bufWriter.close();
                    filerWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        Looper.loop();
    }
}
